<?php

declare(strict_types=1);

namespace app\controller\system;

use app\BaseController;
use app\service\system\UploadService;

/**
 * 文件上传
 */
class Upload extends BaseController
{
    protected $fileService;

    public function initialize()
    {
        $this->fileService = make(UploadService::class);
    }
    /**
     * 图片文件上传（单文件）
     */
    public function uploadImage()
    {
        $this->validate($this->request->file(), [
            'file' => 'file|fileExt:jpg,png,jpeg'
        ], [
            'file.file' => '请选上传文件', 'file.fileExt' => '系统仅支持 jpg,png,jpeg 图片格式'
        ]);
        $path = $this->request->param('type') ?? 'images';
        $file = $this->request->file('file');
        return $this->success($this->fileService->upload($file, $path));
    }
    /**
     * 附件文件上传（单文件）
     */
    public function uploadFile()
    {
        $this->validate($this->request->file(), [
            'file' => 'file|fileExt:xls,xlsx,mp4'
        ], [
            'file.file' => '请选上传文件', 'file.fileExt' => '系统仅支持 xls,xlsx,mp4 格式文件'
        ]);
        $path = $this->request->param('type') ?? 'files';
        $file = $this->request->file('file');
        return $this->success($this->fileService->upload($file, $path));
    }
}
